package com.limao.DBProvider;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.limao.DBDataClass.CrowdPrestigeLogDataClass;
import com.limao.DBDataClass.SysRmbDataClass;
import com.limao.DbConnection.DbConnectionManager;
import com.limao.Model.PublicData;
import com.limao.Tool.CacheClass;

public class CrowdPrestigeLogProvider {

	private static CrowdPrestigeLogProvider instance;

	public static CrowdPrestigeLogProvider getInstance() {
		if(instance==null) instance=new CrowdPrestigeLogProvider();
		return instance;
	}
	
  
	private CrowdPrestigeLogProvider() {

	}

	private CrowdPrestigeLogDataClass encapOrderData(ResultSet rs) throws Exception{
		
		CrowdPrestigeLogDataClass orderData = new CrowdPrestigeLogDataClass();
		
		orderData.logid=rs.getString("logid");
		orderData.userid=rs.getString("userid");
		orderData.payuserid=rs.getString("payuserid");
		orderData.payusername=rs.getString("payusername");
		orderData.cate=rs.getString("cate");
		orderData.type=rs.getString("type");
		orderData.typeAbout=getTypeAbout(CacheClass.getIntValue(orderData.type));
		
		
		orderData.logvalue=rs.getString("logvalue");
		orderData.newlogvalue=rs.getString("newlogvalue");
		orderData.ctime=rs.getString("ctime");
		
		return orderData;
	}

	
	private String getTypeAbout(int cate)
	{
		String str="";
		switch(cate)
		{
		case 0:
			str="注册";
			break;
		case 1:
			str="商城购物";
			break;
		case 2:
			str="每日签到";
			break;	
		case 3:
			str="出金到狸猫豆";
			break;	
		case 4:
			str="狸猫豆购物消耗";
			break;	
		case 5:
			str="增加回购中的狸猫豆";
			break;	
		case 6:
			str="狸猫豆回购成功";
			break;
		case 7:
			str="商城订单取消";
			break;
		case 8:
			str="商城购物增加豆豆";
			break;	
		case 9:
			str="豆豆订单取消退回";
			break;	
		case 10:
			str="管理员调整商城豆豆";
			break;	
		case 11:
			str="管理员调整回购中豆豆";
			break;	
			
		}
		
		return str;
		
		
	}
	

	
	public PublicData Load_PRESTIGE_LOG(int userid,int cate,int curpage)
	{
		PublicData data = new PublicData();
		List<CrowdPrestigeLogDataClass> list = new ArrayList<CrowdPrestigeLogDataClass>();
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int total = 0;
		try {
			String sql = "{call getUserPrestigeLog(?,?,?)}";
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareCall(sql);
			pstmt.setInt(1, userid);
			pstmt.setInt(2, cate);
			pstmt.setInt(3, curpage);
			pstmt.execute();

			boolean hadResults=pstmt.execute();
	        int mm=0;
	        while (hadResults) {
               	rs=pstmt.getResultSet();
	            if(mm==0)
	            {
	               		while(rs != null && rs.next())
	               		{
	               			list.add(encapOrderData(rs));
	               		}
	               		
	             }
	             else if(mm==1)
	             {
	               		while(rs != null && rs.next()){
	               			total=total+rs.getInt(1);
	               		}
	             }
	             hadResults = pstmt.getMoreResults(); //检查是否存在更多结果集
	             mm++;
	       }

			ArrayList<Object> returnlist = new ArrayList<Object>();
			if ((total % 20) > 0) {
				total = (total / 20 + 1);
			} else
				total = (total / 20);

			if (total == 0)
				total = 1;
			data.total = total + "";
			data.curpage = curpage + "";
			returnlist.addAll(list);
			data.list = returnlist;
			data.state = "1";
		} catch (Exception e) {
			list = null;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		
		return data;
	} 

	
	
}
